package cn.jiahua.hospital.controller;


import cn.jiahua.hospital.dto.DoctorDto;
import cn.jiahua.hospital.dto.Result;
import cn.jiahua.hospital.dto.SearchDto;
import cn.jiahua.hospital.dto.TdepartmentsDto;
import cn.jiahua.hospital.pojo.*;
import cn.jiahua.hospital.service.*;
import cn.jiahua.hospital.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "搜索")
@RequestMapping("/api/")
public class SearchController {


    @Resource
    HdidRelationService hdidRelationService;

    @Resource
    DepartmentsService departmentsService;


    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医院ID", required = true, dataType = "int"),
                @ApiImplicitParam(name = "tdepartmentsName", value = "二级科室名称", required = false, dataType = "string"),
            @ApiImplicitParam(name = "name", value = "医生名称", required = false, dataType = "string"),
    })
    @ApiOperation(value="医院内搜索")
    @GetMapping("search/hospital")
    public Result SearchHopital(Integer hospitalId,String tdepartmentsName,String name){
        List<HdidRelation> listHdidRelation = hdidRelationService.selectDT(hospitalId,tdepartmentsName,name);
        return ResultUtils.returnDataSuccess(listHdidRelation);
    }

    @Autowired
    private DoctorService doctorService;

    @ApiOperation(value = "搜索", notes = "搜索")
    @GetMapping("/search")
    public Result searchforIndex(SearchDto searchDto){
        System.out.println(searchDto.getQueryName()+"搜索类型："+searchDto.getType()+"等级："+searchDto.getGradeId()+"医院类型："+searchDto.getTypeId()+"地址："+searchDto.getAddressId());
        //测试--3--0--1--1--0
        Map<String, Object> map = new HashMap<String, Object>();
        if(searchDto.getType()==1){//医院
            map.put("hospitalName",searchDto.getQueryName());

            map.put("gradeId",searchDto.getGradeId());
            map.put("typeId",searchDto.getTypeId());
            map.put("addressId",searchDto.getAddressId());
            List<Hospital> hospitals = doctorService.searchHospital(map);
            if(hospitals!=null){
                return ResultUtils.returnDataSuccess(hospitals);
            }
        }
        else if(searchDto.getType()==2){//科室
            map.put("tdepartmentsName",searchDto.getQueryName());

            map.put("gradeId",searchDto.getGradeId());
            map.put("typeId",searchDto.getTypeId());
            map.put("addressId",searchDto.getAddressId());
            List<TdepartmentsDto> tdepartmentsDtos = doctorService.searchTdepartments(map);
            if(tdepartmentsDtos!=null){
                return ResultUtils.returnDataSuccess(tdepartmentsDtos);
            }

        }
        else if(searchDto.getType()==3){//医生
            map.put("name",searchDto.getQueryName());

            map.put("gradeId",searchDto.getGradeId());
            map.put("typeId",searchDto.getTypeId());
            map.put("addressId",searchDto.getAddressId());
            List<DoctorDto> doctorDtos = doctorService.searchDocotr(map);
            if(doctorDtos!=null){
                return ResultUtils.returnDataSuccess(doctorDtos);
            }
        }

        return ResultUtils.returnFail();



    }

    @ApiImplicitParam(name = "hospitalId", value = "医院ID", required = true, dataType = "String")
    @ApiOperation(value="根据医院ID查询所有一级科室")
    @GetMapping("select/hospital/departments")
    public Result selectHospital(String hospitalId){
        List<Departments> ListDepartments = departmentsService.selectAll(hospitalId);
        return  ResultUtils.returnDataSuccess(ListDepartments);
    }
 

}
